<? 
$link = mysql_connect('localhost', 'root', '');
//$link = mysql_connect('aecabill.accountsupportmysql.com', 'aeca', 'fArC5YyyG8NT9wdC5LXP');
if (!$link) { 
     die('There appeared to be an error while connecting to the website database: ' . mysql_error()); 
}
mysql_select_db(aeca);

//$cmpyID = $_GET['Id'];
$cmpyID= $_POST['cid'];

$query = "select * from tbl_company_type";
$result = mysql_query($query);

$query2 = "select * from tbl_rate_type";
$result2 = mysql_query($query2);

if($cmpyID)
{
$query3 = "select * from tbl_companies where c_id = $cmpyID";
$result3 = mysql_query($query3);
$c_res = mysql_fetch_array($result3);
}

$query4 = "select * from tbl_whis_carriers";
$result4 = mysql_query($query4);

$report_query = "select * from tbl_reporting_type";
$report_result = mysql_query($report_query);

$nickname = $c_res['c_nickname'];

$asrRates_query = "select * from tbl_asr_rates where c_nickname= '$nickname'";
$asrRates_result = mysql_query($asrRates_query);

if($_POST['cmpy_info'] == 'company_info')
{
	extract($_POST);
	if($cmpyID)
	{
		$update_query = "UPDATE tbl_companies SET c_name='$c_name', c_nickname='$c_nickname', c_billing_address1='$c_billing_address1',c_billing_address2='$c_billing_address2'," .
				"c_billing_city='$c_billing_city',c_billing_state='$c_billing_state',c_billing_zipcode='$c_billing_zipcode',c_billing_country='$c_billing_country',c_phone_number='$c_phone_number'," .
				"c_phone_extension='$c_phone_extension',c_fax_number='$c_fax_number',reporting_type='$reporting_type',ct_id='$c_type' WHERE c_id=$cmpyID";
		if(mysql_query($update_query)){?>
		<script>
		$(document).ready(function(){
		 	$("#success").text("Updated Successfully!").show();
			window.location = "index.php?page=company-mang";
		});
		</script>
		<?}	
		$query3 = "select * from tbl_companies where c_id = $cmpyID";
		$result3 = mysql_query($query3);
		$c_res = mysql_fetch_array($result3);	
	}
}
else if($_POST['asr_rates'] == 'asr_rates')
{
	extract($_POST);
	$insert_query = "INSERT INTO tbl_asr_rates (c_nickname,asr_rate_amount,asr_rate_eff_start_date,asr_rate_eff_end_date,rt_rate_type) " .
			"values ('$c_nickname','$asr_rate_amount','$asr_rate_eff_start_date','$asr_rate_eff_end_date','$rt_rate_type')";
	mysql_query($insert_query);
}
else if($_POST['agency_agre'] == 'agency_agre')
{
	extract($_POST);
	$insert_query = "INSERT INTO tbl_agency_agreement (qry_lec_companies,whls_carriers,qry_ixc_companies,effective_start_date,effective_end_date) " .
			"values ('$qry_lec_companies','$whls_carriers','$qry_ixc_companies','$effective_start_date','$effective_end_date')";
	mysql_query($insert_query);
}
else if($_POST['nonpooled'] == 'nonpooled')
{
	extract($_POST);
	$insert_query = "INSERT INTO tbl_np_exchange_entry (c_nickname,np_exchange_entry_name) " .
			"values ('$c_nickname','$np_exchange_entry_name')";
	mysql_query($insert_query);
}

?>
<script>
   $(function() {  
   $(".csubmit").click(function() { 
     // validate and process form here
     $("#edit_cmpyfrm").validate(); 
     var name = 'Ecmpy';
     var cid = $("#cid").val();
     var c_name = $("#c_name").val();
     var c_nickname = $("#c_nickname").val();
     var c_type = $("#c_type").val();
     var reporting_type = $("#reporting_type").val();
     var c_billing_address1 = $("#c_billing_address1").val();
     var c_billing_address2 = $("#c_billing_address2").val();
     var c_billing_city = $("#c_billing_city").val();
     var c_billing_state = $("#c_billing_state").val();
     var c_billing_zipcode = $("#c_billing_zipcode").val();
     var c_billing_country = $("#c_billing_country").val();
     var c_phone_number = $("#c_phone_number").val();
     var c_phone_extension = $("#c_phone_extension").val();
     var c_fax_number = $("#c_fax_number").val();
     if(c_name != '' && c_nickname != '' && c_type != '' && reporting_type != ''){
	     $.post("/aec1/pages/update_cprofile.php",{name:name,cid:cid,c_name:c_name,c_nickname:c_nickname,c_type:c_type,reporting_type:reporting_type,c_billing_address1:c_billing_address1,c_billing_address1:c_billing_address1,c_billing_address2:c_billing_address2,c_billing_city:c_billing_city,c_billing_state:c_billing_state,c_billing_zipcode:c_billing_zipcode,c_billing_country:c_billing_country,c_billing_country:c_billing_country,c_phone_number:c_phone_number,c_phone_extension:c_phone_extension,c_fax_number:c_fax_number},function(data){
//	     	$("#success").text("Updated Successfully!").show();
	     	$( "#dialog:ui-dialog" ).dialog( "destroy" );
			$( "#dialog-message1" ).dialog({
			modal: true,
			buttons: {
				Ok: function() {
					$(this).dialog( "close" );
					window.location = "index.php?page=company-mang";
				}
			}
			});
	     });
	     return false;
     }
   });  
 }); 
  </script>
 
<!-- ui-dialog -->
<div id="dialog-message1" title="Success Message" style="display:none;">
	<p>
		<span class="ui-icon ui-icon-circle-check" style="float:left; margin:0 7px 50px 0;"></span>
		The Company Profile has been updated Successfully.
	</p>
</div>
 
<span id='error'></span><span id='success'></span>
<div id="accordion" style="margin-top:10px;">
<h3><a href="#section3">Company Information</a></h3>
<div>
	<!--<form action="index.php?page=edit-cmpy-profile" method="post" id="edit_cmpyfrm">-->
	<form method="post" id="edit_cmpyfrm" action="">
		<table cellpadding="4">
			<tr>
				<td><strong>Company Name* :</strong></td>
				<td><input type="text" name="c_name" class="required" value="<?=$c_res['c_name'];?>" id="c_name"/></td>
			</tr>
			<tr>
				<td><strong>Company Nickname* :</strong></td>
				<td><input type="text" name="c_nickname" class="required" value="<?=$c_res['c_nickname'];?>" id="c_nickname"/></td>
			</tr>
			<tr>
				<td><strong>Company Type* :</strong></td>
				<td>
				<select name="c_type" id="c_type">
				<? while($row = mysql_fetch_array($result)){?>
					<option value=<? echo $row['ct_id'];?> class="required"
					<? if($c_res['ct_id'] == $row['ct_id']){ echo 'selected="selected"'; } else {} ?>					
					><?=$row['ct_company'];?></option>
				<? }?>				
				</select>
				</td>
			</tr>
			<tr>
				<td><strong>Reporting Type* :</strong></td>
				<td>
				<select name="reporting_type" id="reporting_type">
				<option value="---"> --- </option>
				<? while($rpt = mysql_fetch_array($report_result)){?>
					<option value=<? echo $rpt['rpt_id'];?> class="required"
					<? if($c_res['reporting_type'] == $rpt['rpt_id']){ echo 'selected="selected"'; } else {} ?>
					><?=$rpt['reporting_type'];?></option>
				<? }?>				
				</select>
				</td>
			</tr>
			<tr>
				<td><strong>Billing Address1 :</strong></td>
				<td><input type="text" name="c_billing_address1" value="<?=$c_res['c_billing_address1'];?>" id="c_billing_address1"/></td>
			</tr>
			<tr>
				<td><strong>Billing Address2 :</strong></td>
				<td><input type="text" name="c_billing_address2" value="<?=$c_res['c_billing_address2'];?>" id="c_billing_address2"/></td>
			</tr>
			<tr>
				<td><strong>City :</strong></td>
				<td><input type="text" name="c_billing_city" value="<?=$c_res['c_billing_city'];?>" id="c_billing_city"/></td>
			</tr>
			<tr>
				<td><strong>State :</strong></td>
				<td><input type="text" name="c_billing_state" value="<?=$c_res['c_billing_state'];?>" id="c_billing_state"/></td>
			</tr>
			<tr>
				<td><strong>Zip Code :</strong></td>
				<td><input type="text" name="c_billing_zipcode" value="<?=$c_res['c_billing_zipcode'];?>" id="c_billing_zipcode"/></td>
			</tr>
			<tr>
				<td><strong>Country :</strong></td>
				<td><input type="text" name="c_billing_country" value="<?=$c_res['c_billing_country'];?>" id="c_billing_country"/></td>
			</tr>
			<tr>
				<td><strong>Phone Number :</strong></td>
				<td><input type="text" name="c_phone_number" class="number" value="<?=$c_res['c_phone_number'];?>" id="c_phone_number"/></td>
			</tr>
			<tr>
				<td><strong>Extension :</strong></td>
				<td><input type="text" name="c_phone_extension" class="number" value="<?=$c_res['c_phone_extension'];?>" id="c_phone_extension"/></td>
			</tr>
			<tr>
				<td><strong>Fax Number :</strong></td>
				<td><input type="text" name="c_fax_number" class="number" value="<?=$c_res['c_fax_number'];?>" id="c_fax_number"/></td>
			</tr>
			<tr>
				<td>&nbsp;<input type="hidden" value="company_info" name="cmpy_info" /></td>
				<td>
				<input type="submit" value="submit" name="submit" class="csubmit"/>
				<input type="button" value="Cancel" name="cancel" onclick="location.href='index.php?page=company-mang'"/>
				</td>
			</tr>
			<input type="hidden" name="chidden" value="<?=$cmpyID?>" id="cid"/>
		</table>
	</form>
</div>
<? if($_SESSION['user_level'] == 1){?>	
<h3><a href="#asr-rates">New ASR Rates</a></h3>
<div>
	<form action="index.php?page=edit-cmpy-profile" method="post">
	<table cellpadding="4" cellpadding="0">
		<tr>
			<td><label><strong>LEC Nickname :</strong></label></td>
			<td>
				<input type="text" name="c_nickname" class="required number" value="<?=$c_res['c_nickname'];?>" readonly/>
			</td>
		</tr>
		<tr>
			<td><label><strong>Rate Type :</label></strong></td>
			<td>
			<select name="rt_rate_type">
				<? while($row2 = mysql_fetch_array($result2)){?>
					<option value=<? echo $row2['rt_rate_type'];?>><? echo $row2['rt_rate_type'];?></option>
				<? }?>	
			</select>
			</td>
		</tr>
		<tr>
			<td><label><strong>Rate* :</label></strong></td>
			<td><input type="text" name="asr_rate_amount" class="required number"/></td>
		</tr>
		<tr>
			<td><label><strong>Effective Start Date* :</label></strong></td>
			<td><input type="text" name="asr_rate_eff_start_date" id="datepicker1" class="required"/></td>
		</tr>
		<tr>
			<td><label><strong>Effective End Date* :</label></strong></td>
			<td><input type="text" name="asr_rate_eff_end_date" id="datepicker2" class="required"/></td>
		</tr>
		<tr>
			<td>&nbsp;<input type="hidden" value="asr_rates" name="asr_rates" /></td>
			<td><input type="submit" value="submit" name="submit" /></td>
		</tr>
	</table>
	</form>
</div>
<h3><a href="#agency-agreement">ASR Rates</a></h3>
<div>
	<table border='1'>
	<tr>
		<th>Rate Id</th>
		<th>Company Nickname</th>
		<th>Rate Amount</th>
		<th>Start Date</th>
		<th>End Date</th>
		<th>Rate Type</th>
	</tr>
	<? while($asr = mysql_fetch_array($asrRates_result)){?>
	<tr>
		<td><?=$asr['asr_rate_id'];?></td>
		<td><?=$asr['c_nickname'];?></td>
		<td><?=$asr['asr_rate_amount'];?></td>
		<td><?=$asr['asr_rate_eff_start_date'];?></td>
		<td><?=$asr['asr_rate_eff_end_date'];?></td>
		<td><?=$asr['rt_rate_type'];?></td>
	</tr>
	<? }?>
</table>
</div>
<? }?>
</div>
